/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package tk.mybatis.springboot.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.springboot.common.ResultObject;
import tk.mybatis.springboot.entity.CustomizedTask;
import tk.mybatis.springboot.mapper.TaskMapper;
import tk.mybatis.springboot.service.impl.StatusRootService;
import tk.mybatis.springboot.service.impl.TaskExtendService;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuzh
 * @since 2015-12-19 11:10
 */
@RestController
public class UserInfoController {

     @Autowired
     StatusRootService statusRootService;

     @Autowired
     TaskMapper taskMapper;

     @Autowired
     TaskExtendService taskExtendService;

    @RequestMapping("/api/selectTaskByPage")
    public Object selectTaskByPage(@RequestParam(name="page") Integer page, @RequestParam(name="count") Integer count,
                                   @RequestParam(name="tab") Integer tab){
        List<Map<String, Object>> maps = statusRootService.selectPageByStatus(page, count+50, tab);
        System.out.println("tab页"+tab+"有多少任务:"+maps.size());
        List<CustomizedTask> list = new ArrayList<>();
        if(maps != null && maps.size()>0){
            for(Map<String, Object> item:maps){
                CustomizedTask task = new CustomizedTask();
                task.Status = item.get("Status").toString();
                task.TaskNo = item.get("TaskNo").toString();
                list.add(task);
            }
            int i = taskMapper.batchUpdate(list);
        }
        List<CustomizedTask> customizedTasks = taskExtendService.selectTaskByPage(page, count, tab);
        ResultObject resultObject = new ResultObject();
        resultObject.data = customizedTasks;
        return resultObject;
    }

    @RequestMapping("getStatusByTaskNo")
    public Object getStatusByTaskNo(@RequestParam(name="TaskNo") String TaskNo){
        String url = "jdbc:mysql://localhost:3306/root";
        String user="root";
        String password= "root";
        String status = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,user,password);
            String sql = "select task_result from rt_task_data where 1=1 " +
                    "task_name=?";
             statement = connection.prepareStatement(sql);
            statement.setString(1,TaskNo);

            resultSet = statement.executeQuery();

            while (resultSet.next()){
                status =  resultSet.getString("status");
            }
            System.out.println("状态是"+status);
            Map<String,Object> map = new HashMap<>();
            map.put("status",status);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        return null;
    }

}
